<?php


namespace app\partneradmin\controller;


use app\partneradmin\model\Admin;
use app\common\controller\partneradmin\Base;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;


class Category extends Base
{
    protected $noNeedLogin = [];

    protected $model;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\partneradmin\model\Category;
    }


    /**
     * 查看所有分类（不分页）
     */
    public function index()
    {
        //设置设置请求类型
        if ($this->request->isPost()) {
            $name = $this->request->post('name', '');
            $sort = $this->request->post('sort', 'id');
            $order = $this->request->post('order', 'desc');
            $status = $this->request->post('status', '');
            $field = [
                'id',
                'pid',
                'name',
                'nickname',
                'create_time',
                'update_time',
                'weigh',
                'status',
                'admin_id'
            ];
            $where = [
                'is_del' => 0
            ];
            if ($status !== '') {
                $where['status'] = $status;
            }
            //取出所有分类(TODO Cache handle)
            $allList = $this->model
                ->field($field)
                ->where($where)
                ->order($sort, $order)
                ->select()
                ->toArray();
            $ret = $this->formatCategoryList($name, $allList);

            $this->success('列表', '', $ret);
        }
    }

    /**
     * 格式化分类数组
     * @param $name
     * @param $allList
     * @return array
     */
    public function formatCategoryList($name, $allList)
    {

        $allList = array_column($allList, null, 'id');
        if ($name != '') {
            $retList = search_array_by_key($allList, 'name', $name);
            $retList = array_column($retList, null, 'id');
        } else {
            $retList = $allList;
        }
        //组装数组
        $data = [];
        foreach ($retList as $key => $val) {
            //判断输出结果为父类
            if ($val['pid'] == 0) {
                if (empty($data[$val['id']])) {
                    $data[$val['id']] = $val;
                    $data[$val['id']]['son_list'] = [];
                } else {
                    $val['son_list'] = $data[$val['id']]['son_list'];
                    $data[$val['id']] = $val;
                }
            } else {
                //如果为子类处理逻辑
                if (empty($data[$val['pid']])) {
                    $data[$val['pid']]['son_list'] = [];
                }
                $data[$val['pid']]['son_list'][] = $val;
            }
        }

        //输出最终数组
        $retData = [];
        foreach ($data as $key => $value) {
            if (empty($value['name'])) {
                $son_list = $value['son_list'];
                $value = $allList[$key];
                $value['son_list'] = $son_list;
            }
            if (empty($value['son_list'])) {
                foreach ($allList as $v) {
                    if ($v['pid'] == $value['id']) {
                        $value['son_list'][] = $v;
                    }
                }
            }
            $retData[] = $value;
        }
        return $retData;
    }

    /**
     * 单独获取分类列表
     * @param $pid
     */
    public function getCategoryAll($pid = 0)
    {
        $fields = [
            'id',
            'name',
            'nickname'
        ];
        $where = [
            'is_del' => 0,
            'status' => 1,
            'pid' => $pid
        ];
        $list = $this->model
            ->field(implode(',', $fields))
            ->where($where)
            ->select();
        $this->success('列表', '', ['list' => $list]);
    }
    /**
     * 禁止新增
     */
    public function add()
    {
        $this->error('添加失败！该模块不允许添加');
    }
    /**
     * 禁止编辑
     */
    public function edit($ids = null)
    {
        $this->error('修改失败！该模块合作方不允许编辑');
    }
    /**
     * 禁止删除
     */
    public function del($ids = null)
    {
        $this->error('该模块合作方不允许操作');
    }
}